
%{
  import java.io.*;
%}
      
%type p
%type p2
%type f
%type pe
%type pe2
%type ps
%type ps2
%type b
%type pa
%type t
%type v
%type l
%type l2
%type ls
%type ls2
%type e
%type sc
%type dv
%type dv2
%type ld
%type o
%type o2
%type op
%type op2
%type ob
%type ou
%type i
%type i2
%type i3
%type sif
%type sif2
%type sif3
%type sw
%type sw2


     
%%

p:	f p2;

p2:	f p2 
	|
	;

f:	Token.ID ':' Token.FUNCTION pe ps b ';'

pe:	pa pe2
	|
	;

pe2:	',' pa pe2
	|
	;


ps:	Token.INICIO_PS pa ps2
	| 
	;

ps2:	',' pa ps2
	|
	;

pa:	Token.ID ':' t;

t:	Token.TIPO v;

v:	Token.VECTOR v
	|
	;

b:	'{' ls '}';

ls:	l ls2;
ls2:	l ls2 
	|
	;

l:	l2 ';';
l2:	e
	| sc
	| dv;

e:	Token.ID '=' ld
	| ld;

ld:	Token.ID 
	| o;

o:	op o2;
o2:	ob op o2 
	| 
	;

op:	ou op2;

op2:	Token.ID 
	| i;

i:	Token.ID '(' i2 ')';

i2:	o i3 
	| 
	;
i3:	',' o i3
	| 
	;

dv:	Token.ID ':' t dv2;

dv2:	'=' o;

sc:	sif
	| sw;

sif:	Token.IF o Token.THEN b sif2;

sif2:	Token.ESLSEIF o b sif3
	| sif3;

sif3: Token.ELSE b;

sw:	Token.WHILE '(' o '(' Token.LOOP b sw2;

sw2:	in iq;

in:	Token.IFNEVER b
	|
	;

iq:	Token.IFQUIT b
	|
	;









































      
line:    NL      { if (interactive) System.out.print("Expression: "); }
       | exp NL  { System.out.println(" = " + $1); 
                   if (interactive) System.out.print("Expression: "); }
       ;
      
exp:     NUM                { $$ = $1; }
       | exp '+' exp        { $$ = $1 + $3; }
       | exp '-' exp        { $$ = $1 - $3; }
       | exp '*' exp        { $$ = $1 * $3; }
       | exp '/' exp        { $$ = $1 / $3; }
       | '-' exp  %prec NEG { $$ = -$2; }
       | exp '^' exp        { $$ = Math.pow($1, $3); }
       | '(' exp ')'        { $$ = $2; }
       ;

%%

  private Yylex lexer;


  private int yylex () {
    int yyl_return = -1;
    try {
      yylval = new ParserVal(0);
      yyl_return = lexer.yylex();
    }
    catch (IOException e) {
      System.err.println("IO error :"+e);
    }
    return yyl_return;
  }


  public void yyerror (String error) {
    System.err.println ("Error: " + error);
  }


  public Parser(Reader r) {
    lexer = new Yylex(r, this);
  }


  static boolean interactive;

  public static void main(String args[]) throws IOException {
    System.out.println("BYACC/Java with JFlex Calculator Demo");

    Parser yyparser;
    if ( args.length > 0 ) {
      // parse a file
      yyparser = new Parser(new FileReader(args[0]));
    }
    else {
      // interactive mode
      System.out.println("[Quit with CTRL-D]");
      System.out.print("Expression: ");
      interactive = true;
	    yyparser = new Parser(new InputStreamReader(System.in));
    }

    yyparser.yyparse();
    
    if (interactive) {
      System.out.println();
      System.out.println("Have a nice day");
    }
  }
